/**
 * 
 */
package com.mnurre.shopping.assistant;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author michael
 *
 */
public class CategoriesData {
	
	private static final String TAG = "CategoriesData";
	private Context mContext;
	private DataManager mDataManager;
	private SQLiteDatabase mDb;
		
	public CategoriesData(Context context){
		Log.d(TAG, "In CategoriesData ctor. Setting the context to the callee.");
		mContext = context;
	}
	
	public CategoriesData open() throws SQLException {
		Log.d(TAG, "open called on CategoriesData. Creating a new DatabaseHelper and getting a writable database.");
		mDataManager = new DataManager(mContext);
		mDb = mDataManager.getWritableDatabase();
		Log.d(TAG, "Returning this instance.");
		return this;
	}
	
	public void close() {
		Log.d(TAG, "close called on CategoriesData. Closing the DatabaseHelper.");
		mDataManager.close();
	}
	
	public Cursor getCategories() {
		Log.d(TAG, "getCategories called. Querying the underlying databse the get all categories.");
		Cursor cursor = mDb.query(DataManager.Tables.Categories.TABLE_NAME, 
				new String[] { DataManager.Tables.Categories.COLUMN_ID, 
							DataManager.Tables.Categories.COLUMN_NAME, 
							DataManager.Tables.Categories.COLUMN_IS_TAXABLE }, 
							null, null, null, null, null);
		if(cursor != null){
			cursor.moveToFirst();
		}
		return cursor;
	}
	
	public boolean updateCategory(Long categoryId, String name, int isTaxable){
		Log.d(TAG, "Updating category => " + String.valueOf(categoryId) + ", " + name + ", " + String.valueOf(isTaxable));
		ContentValues values = new ContentValues();
		// values.put(DataManager.Tables.Categories.COLUMN_ID, categoryId);
		values.put(DataManager.Tables.Categories.COLUMN_NAME, name);
		values.put(DataManager.Tables.Categories.COLUMN_IS_TAXABLE, isTaxable);
		Log.d(TAG, "Returning result of query as a boolean.");
		return mDb.update(DataManager.Tables.Categories.TABLE_NAME, 
				values, 
				DataManager.Tables.Categories.COLUMN_ID + "=?", 
				new String[] { String.valueOf(categoryId) }) != -1;
	}
	
	public long insertCategory(String name, int isTaxable) {
		Log.d(TAG, "Adding a new category => " + name + ", " + String.valueOf(isTaxable));
		ContentValues values = new ContentValues();
		values.put(DataManager.Tables.Categories.COLUMN_NAME, name);
		values.put(DataManager.Tables.Categories.COLUMN_IS_TAXABLE, isTaxable);
		Log.d(TAG, "Executing query.");
		return mDb.insert(DataManager.Tables.Categories.TABLE_NAME, null, values);
	}
	
	public Cursor getCategoryById(Long categoryId) throws SQLException {
		Log.d(TAG, "Getting category for id " + String.valueOf(categoryId));
		Cursor cursor = mDb.query(true, DataManager.Tables.Categories.TABLE_NAME, 
				new String[] { DataManager.Tables.Categories.COLUMN_ID, 
					DataManager.Tables.Categories.COLUMN_NAME, 
					DataManager.Tables.Categories.COLUMN_IS_TAXABLE }, 
					DataManager.Tables.Categories.COLUMN_ID + "=" + categoryId, 
					null, null, null, null, null);
		if(cursor != null)
			cursor.moveToFirst();
		return cursor;
	}
	
	public Cursor getCategoryByName(String categoryName){
		Log.d(TAG, "Getting category for name " + categoryName);
		Cursor cursor = mDb.query(true, DataManager.Tables.Categories.TABLE_NAME, 
				new String[] { DataManager.Tables.Categories.COLUMN_ID, 
					DataManager.Tables.Categories.COLUMN_NAME, 
					DataManager.Tables.Categories.COLUMN_IS_TAXABLE }, 
					DataManager.Tables.Categories.COLUMN_NAME + "=" + categoryName, 
					null, null, null, null, null);
		if(cursor != null)
			cursor.moveToFirst();
		return cursor;
	}
	
	public boolean deleteCategory(Long categoryId){
		Log.d(TAG, "Deleting category with id of " + categoryId);
		return mDb.delete(DataManager.Tables.Categories.TABLE_NAME, 
				DataManager.Tables.Categories.COLUMN_ID + "=?", 
				new String[] { String.valueOf(categoryId) }) != 0;
	}
}
